package com.djzhou.gmms.service.message.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.model.announcement.TbAnnouncement;
import com.djzhou.gmms.model.email.TbEmail;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.message.TbMessage;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.message.MessageService;
import com.djzhou.util.DateUtil;

public class MessageServiceImpl extends BaseServiceImpl implements MessageService{

	@Override
	public void add(TbMessage t) {
		messageDAO.insert(t);
	}

	@Override
	public void delete(Integer id) {
		messageDAO.delete(TbMessage.class, id);
		
	}

	@Override
	public TbMessage queryById(Integer id) {
		return messageDAO.queryByID(TbMessage.class, id);
	}

	@Override
	public List<TbMessage> queryList(HashMap<String, Object> hashMap) {
		return messageDAO.query("from TbMessage where deleteFlag=0");
	}

	@Override
	public PageBean<TbMessage> queryPageBean(
			HashMap<String, Object> hashMap, int pageSize, int currentPage) {
		String hql="from TbMessage message where message.deleteFlag=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("startDate")!=null){
				sb.append(" and message.sendDate>='").append(hashMap.get("startDate").toString()+"'");
			}
			if(hashMap.get("endDate")!=null){
				sb.append(" and message.sendDate<='").append(DateUtil.turnDateToString(DateUtil.getAfterDate(DateUtil.turnStringToDate(hashMap.get("endDate").toString(), "yyyy-MM-dd"), 1), "yyyy-MM-dd")+"'");
			}
		}
		sb.append(" order by sendDate desc");
		hql=sb.toString();
		PageBean<TbMessage> pageBean=messageDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbMessage t) {
		messageDAO.update(t);
	}

	@Override
	public PageBean<TbMessage> queryReceiver(Integer userId,int pageSize, int currentPage) {
		String hql="from TbMessage message where message.receiveDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and message.receiver.userId=").append(userId);
		sb.append(" order by message.sendDate desc");
		hql=sb.toString();
		PageBean<TbMessage> pageBean=messageDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public PageBean<TbMessage> querySender(Integer userId,int pageSize, int currentPage) {
		
		String hql="from TbMessage message where message.sendDelete=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		sb.append(" and message.sender.userId=").append(userId);
		sb.append(" order by message.sendDate desc");
		hql=sb.toString();
		PageBean<TbMessage> pageBean=messageDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbMessage> queryMessageByUserId(Integer userId) {
		String hql="from TbMessage message where message.receiver.userId=? and message.messageState=0 and message.sendDelete=0 and message.messageTip=0 order by message.sendDate desc";
		return messageDAO.query(hql, userId);
	}

	@Override
	public int queryMessageCount(Integer userId) {
		String hql="from TbMessage message where message.receiver.userId=? and message.messageState=0 and message.receiveDelete=0";
		return messageDAO.query(hql, userId)==null?0:messageDAO.query(hql, userId).size();
	}

	@Override
	public void addSendMessage(Integer senderId, String receiverIds,
			String messageContent) {
		if(receiverIds.indexOf(",")!=-1){
			String[] array=receiverIds.split(",");
			for(String receiverId:array){
				TbMessage message=new TbMessage();
				message.setMessageContent(messageContent);
				message.setSender(userDAO.queryByID(TbUser.class, senderId));
				message.setReceiver(userDAO.queryByID(TbUser.class, Integer.parseInt(receiverId)));
				message.setMessageState(0);
				message.setMessageTip(0);
				message.setSendDate(new Date());
				message.setSendDelete(0);
				message.setReceiveDelete(0);
				messageDAO.insert(message);
			}
		}else{
			TbMessage message=new TbMessage();
			message.setMessageContent(messageContent);
			message.setSender(userDAO.queryByID(TbUser.class, senderId));
			message.setReceiver(userDAO.queryByID(TbUser.class, Integer.parseInt(receiverIds)));
			message.setMessageState(0);
			message.setMessageTip(0);
			message.setSendDate(new Date());
			message.setSendDelete(0);
			message.setReceiveDelete(0);
			messageDAO.insert(message);
			
		}
		
	}
}
